using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DealerPlatform.Domain.GlobalDto;
using DealerPlatform.Service.ProductApp;
using DealerPlatform.Service.ProductApp.Dto;
using Microsoft.AspNetCore.Mvc;

namespace DealerPlatform.Web.Controllers
{
    public class ProductController : AuthenticationController
    {
        public IProductService Service { get; }
        public ProductController(IProductService service)
        {
            this.Service = service;            
        }

        [HttpGet]
        public async Task<IEnumerable<ProductDto>> Get(
            string BelongTypeNo, 
            string? typeNo, 
            string? searchText, 
            string? sort, 
            string? productProps, 
            OrderType orderType = OrderType.Asc, 
            int pageIndex = 1, 
            int pageSize = 30)
        {
            PageWithSortDto dto = new PageWithSortDto{
                Sort = sort??"Id",
                OrderType = orderType,
                PageIndex = pageIndex,
                PageSize = pageSize
            };
            Dictionary<string, string> dicProductProps = new();
            if(!string.IsNullOrWhiteSpace(productProps))
            {
                var items = productProps.Split("^");
                foreach (var item in items){
                    string key = item.Split('_')[0];
                    string value = item.Split('_')[1];
                    dicProductProps.Add(key, value);
                }
            }
            return await Service.GetProductDto(BelongTypeNo, typeNo, searchText, dicProductProps, dto);
        }

        [HttpGet("type")]
        public async Task<IEnumerable<ProductTypeDto>> GetProductType(string BelongTypeNo)
        {
            return await Service.GetProductType(BelongTypeNo);
        }

        [HttpGet("props")]
        public async Task<Dictionary<string, IEnumerable<string>>> GGetProductProperties(string bigType, string? typeNo)
        {
            return await Service.GetProductProperties(bigType, typeNo);
        }

        [HttpGet("props2")]
        public async Task<List<ProductPropertyDto>> GGetProductProperties2(string bigType, string? typeNo)
        {
            return await Service.GetProductProperties2(bigType, typeNo);
        }

        [HttpGet("belongs")]
        public async Task<List<ProductBelongsDto>> GetProductBelongs()
        {
            return await Service.GetProductBelongs();
        }
    }
}